#!/usr/bin/env bash
#
#
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/bin:/sbin
mount_file=mount.file
disk_ntfs=`lsblk -rf  |awk '$2 == "ntfs"{print $1}'`
points=(/mnt/data1 /mnt/data2 /mnt/data3 /mnt/data4 /mnt/data5 /mnt/data6 /mnt/data7 /mnt/data8 /mnt/data9 /mnt/data10)
tmp_data=/tmp/data
logfile=/var/log/mount.log
unmount_dev=()
index=0

logging () {
    date_time=$(date +"%F %T")
    if [ $1 = "info" ];then
        echo -e "\033[32;1m$date_time info: $2\033[0m" >>$logfile
    elif [ $1 = "warn" ];then
        echo -e "\033[35;1m$date_time warn: $2\033[0m" >>$logfile
    elif [ $1 = "err" ];then
        echo -e "\033[31;1m$date_time error: $2\033[0m" >>$logfile
    fi
}

check_mount_file() {
    device=$1
    if [ -f ${tmp_data}/${mount_file} ];then
        fuuid=`sed -n '1p' ${tmp_data}/${mount_file}`
        point=`sed -n '2p' ${tmp_data}/${mount_file}`
        ruuid=`blkid $device |awk 'NR==1{for(n=1;n<NF;n++){if($n~"UUID"){print $n}}}' |cut -c 6-`
        if [ "$fuuid" = "$ruuid" ];then
            if [ -n "$point" ];then 
                echo "$point"
                return 0
            fi
        fi
    fi
    return 1
}

check_mount_dir() {
    for point in ${points[*]}
    do
        ismount=`df |grep $point`
        if [ -n "$ismount" ];then
            continue
        fi
        mkdir -p ${tmp_data}/disk0{1..8} >>/dev/null 2>&1
        mkdir -p ${tmp_data}/fdisk0{1..8} >>/dev/null 2>&1
        mkdir -p ${tmp_data}/disk1{0..3} >>/dev/null 2>&1
        mkdir -p ${tmp_data}/fdisk1{0..3} >>/dev/null 2>&1
        mkdir -p ${tmp_data}/xin >>/dev/null 2>&1
        mkdir -p ${tmp_data}/ad >>/dev/null 2>&1
        mkdir -p ${tmp_data}/bns >>/dev/null 2>&1
        mkdir -p ${tmp_data}/dj >>/dev/null 2>&1
        >${tmp_data}/${mount_file}
        echo $point
        return 0
    done
    return 1
}

mounter1() {
for disk in $*
do
    mount -t ntfs-3g /dev/$disk $tmp_data || {
        logging "err" "mount -t ntfs-3g /dev/$disk $tmp_data fialed, errno: $?"
        umount $tmp_data
        continue
    }
    logging "info" "mount -t ntfs-3g /dev/$disk $tmp_data successful"
    d=`check_mount_file "/dev/$disk"`
    if [ $? -eq 0 ];then
        ismount=`df |grep $d`
        if [ -n "$ismount" ];then
            rm -f ${tmp_data}/${mount_file}
            logging "warn" "$d already mounted"
            unmount_dev[$index]=$disk
            let index++
            umount $tmp_data || umount $tmp_data &&\
            logging "info" "umount $tmp_data successful"
        else
            umount $tmp_data || umount $tmp_data &&\
            logging "info" "umount $tmp_data successful"
            mount -t ntfs-3g -o defaults,noatime /dev/$disk $d &&\
            logging "info" "mount -t ntfs-3g -o defaults,noatime /dev/$disk $d successful" ||\
            logging "err" "mount -t ntfs-3g -o defaults,noatime /dev/$disk $d failed errno: $?"
        fi
    else
        logging "warn" "not found $mount_file in $disk"
        unmount_dev[$index]=$disk
        let index++
        umount $tmp_data || umount $tmp_data &&\
        logging "info" "umount $tmp_data successful"
    fi
done
}

mounter2() {
    for disk in $*
    do
        mount -t ntfs-3g /dev/$disk $tmp_data || {
            logging "err" "mount -t ntfs-3g /dev/$disk $tmp_data fialed, errno: $?"
            umount $tmp_data
            continue
        }
        logging "info" "mount -t ntfs-3g /dev/$disk $tmp_data successful"
        d=`check_mount_dir`
        if [ $? -eq 0 ];then
            umount $tmp_data || umount $tmp_data &&\
            logging "info" "umount $tmp_data successful"
            mount -t ntfs-3g -o defaults,noatime /dev/$disk $d && {
                logging "info" "mount -t ntfs-3g -o defaults,noatime /dev/$disk $d successful" 
                ruuid=`blkid /dev/$disk |awk 'NR==1{for(n=1;n<NF;n++){if($n~"UUID"){print $n}}}' |cut -c 6-`
                echo -e "${ruuid}\n$d" >${d}/$mount_file
                logging "info" "first mount, device: $d, uuid: $ruuid"
            } || {
                logging "err" "mount -t ntfs-3g -o defaults,noatime /dev/$disk $d failed errno: $?"
            }
        else
            umount $tmp_data || umount $tmp_data &&\
            logging "info" "umount $tmp_data successful"
            logging "warn" "device too many above 10"
        fi
    done
}

[ -d $tmp_data ] ||mkdir -p $tmp_data
mkdir -p /mnt/data{1..10}
df |grep data[1-9] && umount /mnt/data{1..10}
mounter1 $disk_ntfs
mounter2 ${unmount_dev[*]}
